﻿#include "DataProcess.h"

DataProcess::DataProcess(QObject *parent)
	: QObject(parent)
{}

DataProcess::~DataProcess()
{}

void DataProcess::processByteArrayOfBatteryDetailInfo(QByteArray srcArr)
{
	BatteryDetailInfoRom batteryDetailInfo;
	for (int i = 0;i<300;i++)
	{
		batteryDetailInfo.allTemp[i] = static_cast<quint16>(srcArr[i*2+3]) << 8 |
			static_cast<quint16>(srcArr[i*2+4]);
		batteryDetailInfo.allVCell[i] = static_cast<quint16>(srcArr[i*2+603]) << 8 |
			static_cast<quint16>(srcArr[i*2+604]);
	}
	CoreData &coreData = CoreData::getInstance();
	coreData.m_batteryDetailInfo = batteryDetailInfo;
	//CoreData::m_batteryDetailInfo = batteryDetailInfo;

}

void DataProcess::processByteArrayOfProtectParamInfo(QByteArray srcArr)
{
	ProtectParamInfo protectParamInfo;
	protectParamInfo.tov.protectValue = static_cast<quint16>(srcArr[3]) << 8 |
		static_cast<quint16>(srcArr[4]);
	protectParamInfo.tov.protectDelay = static_cast<quint16>(srcArr[5]) << 8 |
		static_cast<quint16>(srcArr[6]);
	protectParamInfo.tov.releaseValue = static_cast<quint16>(srcArr[7]) << 8 |
		static_cast<quint16>(srcArr[8]);
	protectParamInfo.tov.releaseDelay = static_cast<quint16>(srcArr[9]) << 8 |
		static_cast<quint16>(srcArr[10]);
	protectParamInfo.ov.protectValue = static_cast<quint16>(srcArr[11]) << 8 |
		static_cast<quint16>(srcArr[12]);
	protectParamInfo.ov.protectDelay = static_cast<quint16>(srcArr[13]) << 8 |
		static_cast<quint16>(srcArr[14]);
	protectParamInfo.ov.releaseValue = static_cast<quint16>(srcArr[15]) << 8 |
		static_cast<quint16>(srcArr[16]);
	protectParamInfo.ov.releaseDelay = static_cast<quint16>(srcArr[17]) << 8 |
		static_cast<quint16>(srcArr[18]);
	protectParamInfo.tuv.protectValue = static_cast<quint16>(srcArr[19]) << 8 |
		static_cast<quint16>(srcArr[20]);
	protectParamInfo.tuv.protectDelay = static_cast<quint16>(srcArr[21]) << 8 |
		static_cast<quint16>(srcArr[22]);
	protectParamInfo.tuv.releaseValue = static_cast<quint16>(srcArr[23]) << 8 |
		static_cast<quint16>(srcArr[24]);
	protectParamInfo.tuv.releaseDelay = static_cast<quint16>(srcArr[25]) << 8 |
		static_cast<quint16>(srcArr[26]);
	protectParamInfo.uv.protectValue = static_cast<quint16>(srcArr[27]) << 8 |
		static_cast<quint16>(srcArr[28]);
	protectParamInfo.uv.protectDelay = static_cast<quint16>(srcArr[29]) << 8 |
		static_cast<quint16>(srcArr[30]);
	protectParamInfo.uv.releaseValue = static_cast<quint16>(srcArr[31]) << 8 |
		static_cast<quint16>(srcArr[32]);
	protectParamInfo.uv.releaseDelay = static_cast<quint16>(srcArr[33]) << 8 |
		static_cast<quint16>(srcArr[34]);
	protectParamInfo.otd.protectValue = static_cast<quint16>(srcArr[35]) << 8 |
		static_cast<quint16>(srcArr[36]);
	protectParamInfo.otd.protectDelay = static_cast<quint16>(srcArr[37]) << 8 |
		static_cast<quint16>(srcArr[38]);
	protectParamInfo.otd.releaseValue = static_cast<quint16>(srcArr[39]) << 8 |
		static_cast<quint16>(srcArr[40]);
	protectParamInfo.otd.releaseDelay = static_cast<quint16>(srcArr[41]) << 8 |
		static_cast<quint16>(srcArr[42]);
	protectParamInfo.otc.protectValue = static_cast<quint16>(srcArr[43]) << 8 |
		static_cast<quint16>(srcArr[44]);
	protectParamInfo.otc.protectDelay = static_cast<quint16>(srcArr[45]) << 8 |
		static_cast<quint16>(srcArr[46]);
	protectParamInfo.otc.releaseValue = static_cast<quint16>(srcArr[47]) << 8 |
		static_cast<quint16>(srcArr[48]);
	protectParamInfo.otc.releaseDelay = static_cast<quint16>(srcArr[49]) << 8 |
		static_cast<quint16>(srcArr[50]);
	protectParamInfo.utd.protectValue = static_cast<quint16>(srcArr[51]) << 8 |
		static_cast<quint16>(srcArr[52]);
	protectParamInfo.utd.protectDelay = static_cast<quint16>(srcArr[53]) << 8 |
		static_cast<quint16>(srcArr[54]);
	protectParamInfo.utd.releaseValue = static_cast<quint16>(srcArr[55]) << 8 |
		static_cast<quint16>(srcArr[56]);
	protectParamInfo.utd.releaseDelay = static_cast<quint16>(srcArr[57]) << 8 |
		static_cast<quint16>(srcArr[58]);
	protectParamInfo.utc.protectValue = static_cast<quint16>(srcArr[59]) << 8 |
		static_cast<quint16>(srcArr[60]);
	protectParamInfo.utc.protectDelay = static_cast<quint16>(srcArr[61]) << 8 |
		static_cast<quint16>(srcArr[62]);
	protectParamInfo.utc.releaseValue = static_cast<quint16>(srcArr[63]) << 8 |
		static_cast<quint16>(srcArr[64]);
	protectParamInfo.utc.releaseDelay = (static_cast<quint16>(srcArr[65]) << 8 |
		static_cast<quint16>(srcArr[66]));
	protectParamInfo.ocd1.protectValue = (static_cast<quint16>(srcArr[67]) << 8 |
		static_cast<quint16>(srcArr[68]));
	protectParamInfo.ocd1.protectDelay = (static_cast<quint16>(srcArr[69]) << 8 |
		static_cast<quint16>(srcArr[70]));
	protectParamInfo.ocd1.releaseValue = (static_cast<quint16>(srcArr[71]) << 8 |
		static_cast<quint16>(srcArr[72]));
	protectParamInfo.ocd1.releaseDelay = (static_cast<quint16>(srcArr[73]) << 8 |
		static_cast<quint16>(srcArr[74]));
	int i = 75;
	protectParamInfo.occ.protectValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.occ.protectDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.occ.releaseValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.occ.releaseDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.ocd2.protectValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.ocd2.protectDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.ocd2.releaseValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.ocd2.releaseDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));

	protectParamInfo.sc = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	//跳过压差保护
	i++;
	protectParamInfo.otMos.protectValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.otMos.protectDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.otMos.releaseValue = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));
	protectParamInfo.otMos.releaseDelay = (static_cast<quint16>(srcArr[i++]) << 8 |
		static_cast<quint16>(srcArr[i++]));

	CoreData &coreData = CoreData::getInstance();
	coreData.m_protectParamInfo = protectParamInfo;

	emit sendProtectParamInfo(protectParamInfo);
}

void DataProcess::processByteArrayOfFactoryParamInfo(QByteArray srcArr)
{
	FactoryParamInfo factoryParamInfo;
	int i = 2;
	factoryParamInfo.UART1_ADDR = static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]);
	factoryParamInfo.UART1_Bound = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART2_ADDR = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART2_Bound = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART3_ADDR = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART3_Bound = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART4_ADDR = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.UART4_Bound = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CAN1_BPS = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CAN2_BPS = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CAN3_BPS = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CAN4_BPS = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	i = i + 6;
	factoryParamInfo.PACK_IterationFcc = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CHG_Voltage = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CHG_Current = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.REALRC_H = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.REALRC_L = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.OFFECT_I = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.SampRes = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.HOT_MODE = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.HOT_ON_TEMP = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.HOT_OFF_TEMP = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CHG_DSG_MODE = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.KEY_MODE = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.CD_ForceTime = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.Battery_Type = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.Bal_Res = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.Bal_Type = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.Bal_ONVol = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.Bal_Vdiff = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	factoryParamInfo.LowPower_BEEP_EN = (static_cast<quint16>(srcArr[++i]) << 8 |
		static_cast<quint16>(srcArr[++i]));
	for (int j = 0;j<30;j++)
	{
		factoryParamInfo.AFE[j] = (static_cast<quint16>(srcArr[++i]) << 8 |
			static_cast<quint16>(srcArr[++i]));
	}
	for (int k = 0;k<30;k++)
	{
		factoryParamInfo.ALLTEMPNUM[k] = (static_cast<quint16>(srcArr[++i]) << 8 |
			static_cast<quint16>(srcArr[++i]));
	}

	emit sendFactoryParamInfo(factoryParamInfo);
}

QVector<QString> DataProcess::getProtectReasonInfo(quint16 data)
{
	QVector<QString> protectInfoVec;
	for (int i = 15; i >= 0 ; i--)
	{
		bool bitValue = (data >> i) & 1;
		if (bitValue)
		{
			switch (i)
			{
			case 0:
				protectInfoVec.append(u8"短路保护");
				break;
			case 1:
				protectInfoVec.append(u8"压差保护");
				break;
			case 2:
				protectInfoVec.append(u8"放电过流二级");
				break;
			case 3:
				protectInfoVec.append(u8"充电过流");
				break;
			case 4:
				protectInfoVec.append(u8"放电过流");
				break;
			case 5:
				protectInfoVec.append(u8"总压过压");
				break;
			case 6:
				protectInfoVec.append(u8"总压欠压");
				break;
			case 7:
				protectInfoVec.append(u8"单体欠压");
				break;
			case 8:
				protectInfoVec.append(u8"充电高温");
				break;
			case 9:
				protectInfoVec.append(u8"充电低温");
				break;
			case 11:
				protectInfoVec.append(u8"放电高温");
				break;
			case 12:
				protectInfoVec.append(u8"放电低温");
				break;
			case 13:
				protectInfoVec.append(u8"采集芯片故障");
				break;
			case 14:
				protectInfoVec.append(u8"绝缘保护");
				break;
			case 15:
				protectInfoVec.append(u8"Mos高温");
				break;
				
			default:
				break;
			}
		}
	}
	return protectInfoVec;
}

//(i-1)*2+3 (i-1)*2+4
void DataProcess::processByteArrayOfBatteryInfo(QByteArray srcArr)
{
	BatteryInfoRom batteryInfo;
	batteryInfo.soh = static_cast<quint16>(srcArr[15]) << 8 |
		static_cast<quint16>(srcArr[16]);
	batteryInfo.voltage = static_cast<quint16>(srcArr[17]) << 8 |
		static_cast<quint16>(srcArr[18]);
	batteryInfo.curCade = static_cast<quint16>(srcArr[19]) << 8 |
		static_cast<quint16>(srcArr[20]);
	batteryInfo.tmax = static_cast<quint16>(srcArr[27]) << 8 |
		static_cast<quint16>(srcArr[28]);
	batteryInfo.tmin = static_cast<quint16>(srcArr[29]) << 8 |
		static_cast<quint16>(srcArr[30]);
	batteryInfo.tmaxNo = static_cast<quint16>(srcArr[31]) << 8 |
		static_cast<quint16>(srcArr[32]);
	batteryInfo.tminNo = static_cast<quint16>(srcArr[33]) << 8 |
		static_cast<quint16>(srcArr[34]);
	batteryInfo.vmax = static_cast<quint16>(srcArr[35]) << 8 |
		static_cast<quint16>(srcArr[36]);
	batteryInfo.vmin = static_cast<quint16>(srcArr[37]) << 8 |
		static_cast<quint16>(srcArr[38]);
	batteryInfo.vmaxNo = static_cast<quint16>(srcArr[39]) << 8 |
		static_cast<quint16>(srcArr[40]);
	batteryInfo.vminNo = static_cast<quint16>(srcArr[41]) << 8 |
		static_cast<quint16>(srcArr[42]);
	batteryInfo.rsoc = static_cast<quint16>(srcArr[43]) << 8 |
		static_cast<quint16>(srcArr[44]);
	batteryInfo.fcc = static_cast<quint16>(srcArr[45]) << 8 |
		static_cast<quint16>(srcArr[46]);
	batteryInfo.rc = static_cast<quint16>(srcArr[47]) << 8 |
		static_cast<quint16>(srcArr[48]);
	batteryInfo.cycleCount = static_cast<quint16>(srcArr[49]) << 8 |
		static_cast<quint16>(srcArr[50]);
	batteryInfo.pack_RWH = static_cast<qint16>(srcArr[65]) << 8 |
		static_cast<qint16>(srcArr[66]);
	batteryInfo.vMaxMinDiff = static_cast<quint16>(srcArr[67]) << 8 |
		static_cast<quint16>(srcArr[68]);
	batteryInfo.vSum = static_cast<quint16>(srcArr[69]) << 8 |
		static_cast<quint16>(srcArr[70]);
	batteryInfo.tsum = static_cast<quint16>(srcArr[73]) << 8 |
		static_cast<quint16>(srcArr[74]);

	batteryInfo.protectInfoVec = getProtectReasonInfo(static_cast<quint16>(srcArr[53]) << 8 |
		static_cast<quint16>(srcArr[54]));

	CoreData& coreData = CoreData::getInstance();
	coreData.m_batteryInfo = batteryInfo;
	//emit readBatteryInfoFinished();
}
